package com.example.administrator.timertask;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.example.administrator.timertask.weight.MarqueeView;
import com.example.administrator.timertask.weight.ScrollForeverTextView;

public class TestActivity extends AppCompatActivity {

    private long leftTime = 60;
    TextView textView;
    Handler handler = new Handler();
    private MarqueeView marqueeText;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        initView();
    }

    private void initView() {
        textView = findViewById(R.id.time);
        marqueeText = findViewById(R.id.marquee_view);

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        handler.postDelayed(update_thread, 1000);
       initMarqueeView();
    }

    private void initMarqueeView() {
        View view = (LayoutInflater.from(TestActivity.this)).inflate(R.layout.scroll_textview, null);
        marqueeText.addViewInQueue(view);

        marqueeText.requestFocus();//获取焦点
        marqueeText.setScrollSpeed(10);//滚动速度
        marqueeText.setScrollDirection(MarqueeView.RIGHT_TO_LEFT);//滚动方向
        marqueeText.setViewMargin(10);//按钮间隔
        marqueeText.startScroll();
    }


    Runnable update_thread = new Runnable() {
        @Override
        public void run() {
            leftTime--;
            Log.e("leftTime=", leftTime + "");
            if (leftTime > 0) {
                //倒计时效果展示
                String formatLongToTimeStr = formatLongToTimeStr(leftTime);
                textView.setText(formatLongToTimeStr);
                //每一秒执行一次
                handler.postDelayed(this, 1000);
            } else {//倒计时结束
                // 处理业务流程 //发送消息，结束倒计时
                Message message = new Message();
                message.what = 1;
                handlerStop.sendMessage(message);
            }
        }
    };


    final Handler handlerStop = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    leftTime = 0;
                    handler.removeCallbacks(update_thread);
                    break;
            }
            super.handleMessage(msg);
        }
    };

    public String formatLongToTimeStr(Long l) {
        int hour = 0;
        int minute = 0;
        int second = 0;
        second = l.intValue();
        if (second > 60) {
            minute = second / 60; //取整
            second = second % 60; //取余
        }
        if (minute > 60) {
            hour = minute / 60;
            minute = minute % 60;
        }
        String strtime = "剩余：" + hour + "小时" + minute + "分" + second + "秒";
        return strtime;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        leftTime = 0;
        handler.removeCallbacks(update_thread);
    }

}
